﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

namespace Sport_Shop.DAL
{
    public class DataAccess
    {
        #region 1. Khai báo các thành viên dữ liệu
        SqlConnection conn;
        SqlCommand cmd;
        #endregion

        #region 2. Các phương thức khởi tạo
        public DataAccess()
        {
            conn = new SqlConnection(@"server=TUAN\SQLEXPRESS; database=QLDCTT; integrated security = true;");

        }
        #endregion

        #region 3. Các phương thức thao tác với CSDL

       
        private void Open()
        {
            if (conn.State == System.Data.ConnectionState.Closed)
                conn.Open();
        }

      
        private void Close()
        {
            if (conn.State == System.Data.ConnectionState.Open)
                conn.Close();
        }
       
       
        public object GetValue(string query)
        {
            Open();
            cmd = new SqlCommand(query, conn);
            return cmd.ExecuteScalar();
        }

        public DataSet GetDataList(string select)
        {
            SqlDataAdapter da = new SqlDataAdapter(select, conn);
            DataSet dt = new DataSet();
            da.Fill(dt);
            Close();
            return dt;
        }

        public DataTable GetDataTable(string select)
        {
            SqlDataAdapter da = new SqlDataAdapter(select, conn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            Close();
            return dt;
        }

        public int ExecuteNonQuery(string query)
        {
            Open();
            cmd = new SqlCommand(query, conn);
            return cmd.ExecuteNonQuery();
        }



        #endregion
    
    }
}